﻿using StoreSystem.ViewModels;

namespace StoreSystem.Models
{
    /// <summary>
    /// 仓库房间项类，表示仓库中的一个房间及其相关信息。
    /// </summary>
    public class StoreRoomInfo
    {
        /// <summary>
        /// 房间的唯一标识符。
        /// </summary>
        public int RoomNO { get; set; }

        /// <summary>
        /// 仓库编号。
        /// </summary>
        public int StoreNO { get; set; }

        /// <summary>
        /// 房间名称。
        /// </summary>
        public string RoomName { get; set; }

        /// <summary>
        /// 房间状态标识符。
        /// </summary>
        public int RoomState { get; set; } = 0;

        public THDataInfo LocalTH { get; set; } = null;

        /// <summary>
        /// 从属设备ID。
        /// </summary>
        public byte SlaveID { get; set; }

        /// <summary>
        /// 温度传感器地址。
        /// </summary>
        public ushort TemperatureAddress { get; set; }

        /// <summary>
        /// 湿度传感器地址。
        /// </summary>
        public ushort HumidityAddress { get; set; }

        internal void UpdateState(AlarmSetInfo alarmSet)
        {
            if (LocalTH == null)
            {
                RoomState = 0;
                return;
            }
            bool tError = false;
            bool hError = false;
            if (alarmSet.HTValue <= LocalTH.Temperature || alarmSet.LTValue >= LocalTH.Temperature)
                tError = true;
            if (alarmSet.HHValue <= LocalTH.Humidity || alarmSet.LHValue >= LocalTH.Humidity)
                hError = true;
            if (tError && hError)
                RoomState = 4;
            else if (tError == true && hError == false)
                RoomState = 2;
            else if (tError == false && hError == true)
                RoomState = 3;
            else
            {
                RoomState = 1;
            }

            LocalTH.State = RoomState;
        }
    }
}